<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>编辑采购单产品明细</title>
    </head>
    <body class="easyui-layout">
    <script>
        $(function () {
            $("#editPur-save").linkbutton({onClick:function () {
                    //使用ajax向后台发送表格数据
                    var JSONObj=new Object();
                    var datarows = $("#pdg-add").datagrid("getData");
                    var mydata =[];
                    for(var i=0;i<datarows.total;i++){
                        obj  =new Object();
                        obj.proid =datarows.rows[i].proid;
                        obj.ppdid =null;
                        obj.pidfk = $("#hut-purid").textbox("getValue");
                        obj.ppmoney =datarows.rows[i].ppmoney;
                        obj.ppcost = datarows.rows[i].ppcost;
                        obj.ppcount = datarows.rows[i].ppcount;
                        mydata.push(obj);
                    }
                    JSONObj.jsonStr= JSON.stringify(mydata);
                    JSONObj.purid = $("#hut-purid").textbox("getValue");
                    $.ajax({
                        url:'sendData',
                        type:'post',
                        dataType:'json',
                        data:JSONObj,
                        success:function (data) {
                            $("#editPurchase-form").form({
                                method:'post',
                                onSubmit:function () {
                                    return $(this).form("validate");
                                },
                                success:function (data) {
                                    var data = eval("("+data+")");
                                    if(data.success){
                                        $("#editPurchase-form").form("clear");
                                        var row = $("#purdg").datagrid("getSelected");
                                        $("#editPur"+row.purid).dialog("close");
                                        $.messager.alert("success","编辑采购信息成功!","info");
                                        $("#purdg").datagrid({
                                            url:'allPurPaged',
                                            method:'post'
                                        });
                                        $("#purdg").datagrid("reload");
                                    }else{
                                        $.messager.alert("failed",data.message,"error");
                                    }

                                }
                            }).submit();
                        },
                        error:function (error) {
                            console.log(error);
                        }
                    });
                }});

            $("tboby tr").addClass("datagrid-row");
            //清空
            $("#pdg-add-cleaerLine").linkbutton({
                onClick: function () {
                    $.messager.confirm("继续清空提示", "确定要清空所有采购产品明细吗？", function (t) {
                        {
                            if (t) {
                                if ($("#pdg-add").datagrid("getData").total >= 1) {
                                    //清空datagrid数据
                                    $('#pdg-add').datagrid('loadData', {total: 0, rows: []});
                                    $.messager.alert("SUCCESS", "清空成功！", "info");
                                } else {
                                    $.messager.alert("Failed", "清空失败！无采购产品明细！", "warning");
                                }
                            } else {
                            }
                        }
                    });
                }
            });
            //批量删除采购产品明细
            $("#pdg-add-delLine").linkbutton({
                onClick:function () {
                    $.messager.confirm("继续删除提示","确定要删除所有选中采购产品明细吗？",function (t) {
                            if(t){
                                if($("#pdg-add").datagrid("getChecked").length>=1) {
                                    //批量删除datagrid数据
                                    var rows = $("#pdg-add").datagrid("getChecked");
                                    for (var i = rows.length - 1; i >= 0; i--) {
                                        var rowIndex = $('#pdg-add').datagrid('getRowIndex', rows[i]);
                                        $('#pdg-add').datagrid('deleteRow', rowIndex);
                                    }
                                    // $("#pdg-add").datagrid("reload");
                                    $.messager.alert("SUCCESS","删除成功！","info");
                                }else {
                                    $.messager.alert("Failed","删除失败！未选中行！","warning");
                                }
                            }
                        else{

                        }
                    });
                }
            });

            $("#pdg-add-reloadLine").linkbutton({onClick:function () {
                    var nowData = $.parseJSON(JSON.stringify($("#pdg-add").datagrid("getData")));
                    $('#pdg-add').datagrid('loadData', nowData);
                }});

            $("#editPurchase-form").form({
                onLoadSuccess: function () {
                    $("#hut-purid").next().hide();
                    $("#clientname").next().hide();
                }
            });

            $("#pdg-add").datagrid({
                onBeforeLoad:function () {
                    var queryParams = $("#pdg-add").datagrid('options').queryParams;
                    queryParams.purid = $("#purdg").datagrid("getSelected").purid;
                }
            });

            $("#editPur-close").linkbutton({onClick:function () {
                    var row = $("#purdg").datagrid("getSelected");
                    $("#editPur"+row.purid).dialog("close");
                }});

            $("#client-search-edit").combobox({
                onChange:function (newValue,oldValue) {
                    var queryParams = $("#contact-search-edit").combobox('options').queryParams;
                    queryParams.cid = $("#client-search-edit").val();
                }
            });

            $("#client-search-edit").combobox({
                onBeforeLoad: function(param){
                    if(param == null || param.q == null || param.q.replace(/ /g, '') == ''){
                        var value = $(this).combobox('getValue');
                        if(value){// 修改的时候才会出现q为空而value不为空
                            param.id = value;
                            return true;
                        }
                        return false;
                    }
                }});

            $("#contact-search-edit").combobox({
                onBeforeLoad: function(param){
                    if(param == null || param.q == null || param.q.replace(/ /g, '') == ''){
                        var value = $(this).combobox('getValue');
                        if(value){// 修改的时候才会出现q为空而value不为空
                            param.id = value;
                            return true;
                        }
                        return false;
                    }
                }});

            $("#pdg-add-editLine").linkbutton({onClick:function () {
                    if($("#pdg-add").datagrid("getChecked").length>1){
                        $.messager.alert("Failed","不可选中多行！","info");
                    }else if($("#pdg-add").datagrid("getChecked").length==0){
                        $.messager.alert("Failed","未选中行！","info");
                    }else{
                        $("<div>").attr("id",'pdg-add-editLine-dialog').dialog(
                            {
                                iconCls:'icon-save2',
                                title : '编辑采购产品明细',
                                width : '60%',
                                height : '55%',
                                closed : false,
                                cache : false,
                                draggable : false,
                                href : 'to-editLine-dialog',
                                modal : true,
                                shadow:true,
                                onClose : function()
                                {
                                    $(this).dialog('destroy');
                                },
                                onLoad:function () {
                                    //使用row填充form的内容
                                    $("#editAddLinePro-form").form("load",$("#pdg-add").datagrid("getSelected"));
                                }
                            });
                        $("#pdg-add-editLine-dialog").dialog("open");
                    }
                }});
        });
        function formatImg(value,row,index){
            return '<a href="to-proImgZoomIn?value='+value+'\" target="_blank"><img  title=\"点击查看高清图\" alt=\"图片暂时无法显示\"  src=\"'+'productionsphotosZoomOut\\'+value+'\"/></a>';
        }
        function formatC(value,row,index) {
            if(value==""||value==null){
                return'<span style="font-size:12px;color:red;">暂无分类</span>';
            }else {
                return value;
            }
        }
        function formatM(value,row,index){
            if(value!=null){
                return "￥"+value;
            }else{
                return value;
            }
        }
    </script>
    <div data-options="region:'north'" style="height: 280px;">
        <form id="editPurchase-form" action="editPurchase-form-sub" method="post">
            <div class="div-title-hut"> --------------------------------<span class="span-title">采购单基本信息</span>---------------------------</div>
            <div class="ddt">
                <input id="hut-purid" name="purid" class="easyui-textbox" data-options="border:false"/>
                <input name="purtheme" class="easyui-textbox" data-options="label:'采购主题',required:true,width:300"><div style="display: inline-block;margin-left: 5%;"></div>
                <input id="clientname" name="clientname" class="easyui-textbox" data-options="border:false"/>
                <input id="client-search-edit" name="clientId" class="easyui-combobox" data-options="required:true,prompt:'模糊供应商名称',url:'getAllClientBymh',
                label:'供应商(客户)',valueField:'cid',textField:'cname',method:'post',editable:true,panelHeight:'auto',width:300,mode:'remote'" >
            </div>
            <div class="ddt">
                <input id="contact-search-edit" name="contactId" class="easyui-combobox" data-options="required:false,prompt:'输入任意取值',url:'getAllContactBymh',
                label:'供应商联系人',valueField:'contactid',textField:'contactName',method:'post',editable:true,panelHeight:'auto',width:300,mode:'remote'" ><div style="display: inline-block;margin-left: 5%;"></div>
                <select id="user-search" class="easyui-combogrid" name="purUserId" style="width:300px;"
                        data-options="
                    label:'经手人',
                    panelWidth:330,
                    prompt:'模糊经手人名称或昵称',
                    required:true,
                    panelHeight:'auto',
                    idField:'handmanId',
                    textField:'handman',
                    mode:'remote',
                    method:'post',
                    url:'findUserBypiny',
                    columns:[[
                        {field:'handmanId',title:'用户编号',width:60},
                        {field:'nickname',title:'用户昵称',width:100},
                        {field:'handman',title:'用户实名',width:100}
                    ]]
                "></select>
            </div>
            <div class="ddt">
                <input id="purdate" name="purdate" class="easyui-datebox" data-options="label:'采购日期',required:true,width:300"><div style="display: inline-block;margin-left: 5%;"></div>
                <input id="purexcepttime" name="purexcepttime" class="easyui-datebox" data-options="label:'预计到货日期',required:false,width:300">
            </div>
            <div class="ddt">
                <span>备注:</span><br>
                <input id="remarks" name="remarks" class="easyui-textbox" data-options="required:false,width:650,height:45,multiline:true">
            </div>
        </form>
    </div>
    <div data-options="region:'center'" style="height: 200px;">
            <table id="pdg-add" class="easyui-datagrid table-chance" data-options="fit:true,striped:true,fitColumns:true,checkOnSelect:true,
           autoRowHeight:true,nowrap:false,pagination:true,toolbar:'#pdg-add-tools',
        pageList:[5,10,15,20],pageSize:5,scrollbarSize:0,url:'allppd',method:'post'">
                <thead>
                <tr class="datagrid-header-row">
                    <th data-options="field:'code',width:5,align:'center',checkbox:true">编码</th>
                    <th data-options="field:'ppdid',width:5,align:'center',hidden:true">采购详情ID</th>
                    <th data-options="field:'proid',width:5,align:'center'">产品ID</th>
                    <th data-options="field:'pidfk',width:5,align:'center',hidden:true">采购单id</th>
                    <th data-options="field:'pname',width:15,align:'center'">产品名称</th>
                    <th data-options="field:'pimg',width:10,align:'center'" formatter="formatImg">产品图片</th>
                    <th data-options="field:'ppmoney',width:10,align:'center'" formatter="formatM">产品采购单价</th>
                    <th data-options="field:'ppcount',width:10,align:'center'" formatter="formatM">产品采购价格</th>
                    <th data-options="field:'ppcost',width:10,align:'center'" formatter="formatM">产品采购总价</th>
                    <%--<th data-options="field:'pcategory',width:10,align:'center',hidden:true">产品分类id</th>--%>
                    <th data-options="field:'pCategoryname',width:15,align:'center'" formatter="formatC">产品分类名</th>
                    <%--<th data-options="field:'poprice',width:10,align:'center'" formatter="formatM">产品成本价</th>--%>
                    <%--<th data-options="field:'pbirthday',width:10,align:'center'">生产日期</th>--%>
                    <%--<th data-options="field:'pouttime',width:10,align:'center'">失效日期</th>--%>
                    <th data-options="field:'pcompany',width:20,align:'center'">生产厂家</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
            <!--工具栏-->
            <div id="pdg-add-tools">
                <a id="pdg-add-addLine" href="#" onclick="createDialog('pdg-add-addLine-dialog','查找商品信息','to-addLine-dialog')"  class="easyui-linkbutton" plain="true" iconCls="icon-mini_add2" >添加产品</a>
                <a id="pdg-add-delLine" href="#" class="easyui-linkbutton" plain="true" iconCls="icon-my_delete" >删除产品</a>
                <a id="pdg-add-editLine" href="#"  class="easyui-linkbutton" plain="true" iconCls="icon-my_edit" >修改产品</a>
                <a id="pdg-add-cleaerLine" href="#" class="easyui-linkbutton" plain="true" iconCls="icon-clear">清空所有</a>
                <a id="pdg-add-reloadLine" href="#" class="easyui-linkbutton" plain="true" iconCls="icon-refresh">刷新</a>
            </div>
    </div>
        <div class="btn" style="text-align:center;margin-top:50px;">
            <a id="editPur-save" class="easyui-linkbutton " data-options="iconCls:'icon-ok2'">修改</a><div style="width: 20px;display: inline-block"></div>
            <a id="editPur-close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel2'">取消</a>
        </div>
    </body>
</html>
